package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfGoodsClassDAO;
import com.ljkj.common.db.domain.ZjfGoodsClass;
import com.ljkj.common.db.domain.ZjfGoodsType;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfGoodsClassService {
	
	private ZjfGoodsClassDAO zjfGoodsClassDAO;
	private TLogService tLogService;
	private ZjfGoodsTypeService zjfGoodsTypeService;


	/**
	 * 获取产品分类
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getGoodsClassByProperties(String params,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfGoodsClass.class};
		String[] propertyNames=null;
		Object[] values=null;
		String[] likePropertyNames = null;
		Object[] likevalues= null;
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfGoodsClassDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfGoodsClassDAO.setPageMaxCount(pageSize);
		List<ZjfGoodsClass> zjfGoodsClasses = this.zjfGoodsClassDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGoodsClass zjfGoodsClass : zjfGoodsClasses){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("classId",zjfGoodsClass.getClassId());
			entry_map.put("classTypeId", zjfGoodsClass.getClassTypeId());
			if(zjfGoodsClass.getClassTypeId() != null){
				ZjfGoodsType zjfGoodsType = this.zjfGoodsTypeService.geZjfGoodsTypeById(zjfGoodsClass.getClassTypeId());
				if(zjfGoodsType != null){
					entry_map.put("classTypeName", zjfGoodsType.getTypeName());
				}
			}
			entry_map.put("className", zjfGoodsClass.getClassName());
			
			if(zjfGoodsClass.getClassParentId() != null){
				ZjfGoodsClass temp_class = this.getGoodsClassById(zjfGoodsClass.getClassParentId());
				if(temp_class != null){
					entry_map.put("classParentName", temp_class.getClassName());
				}
			}
			entry_map.put("classLevel",zjfGoodsClass.getClassLevel());
			
			entry_map.put("createTime",zjfGoodsClass.getCreateTime());
			entry_map.put("createUser", zjfGoodsClass.getCreateUser());
			entry_map.put("updateTime", zjfGoodsClass.getUpdateTime());
			entry_map.put("updateUser", zjfGoodsClass.getUpdateUser());
			entry_map.put("disabledState", zjfGoodsClass.getDisabledState());
			entry_map.put("classIcon", zjfGoodsClass.getClassIcon());
			entry_map.put("classSequence", zjfGoodsClass.getClassSequence());
			entry_map.put("classRecommend", zjfGoodsClass.getClassRecommend());
			entry_map.put("classParentId", zjfGoodsClass.getClassParentId());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	public String getGoodsClassForSonClass(){

		List<ZjfGoodsClass> zjfGoodsClasses = this.zjfGoodsClassDAO.findByProperty("disabledState", 0);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfGoodsClass zjfGoodsClass : zjfGoodsClasses){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("classId",zjfGoodsClass.getClassId());
			entry_map.put("className", zjfGoodsClass.getClassName());
			
			entry_list.add(entry_map);
		}
		
        return JSON.toJSONString(entry_list);
	}
	
	
	
	/**
	 * 新增或更新分类
	 * @param zjfGoodsClass
	 * @return
	 */
	public String addOrEditGoodsClassInfo(ZjfGoodsClass zjfGoodsClass,Tuser tuser){
		
		Tlog tlog = new Tlog();
		
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfGoodsClass.getClassId() == null){
			
			
			tlog.setOpType(1);
			tlog.setContent("新增分类："+ zjfGoodsClass.getClassName());
			zjfGoodsClass.setClassId(RandomCode.makeCode());
			zjfGoodsClass.setCreateTime(DateTime.getDateTime());
			zjfGoodsClass.setCreateUser(tuser.getLoginName());
			if(this.saveNewGoodsClass(zjfGoodsClass)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				
				if(zjfGoodsClass.getDisabledState()!=null && zjfGoodsClass.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("分类状态："+zjfGoodsClass.getClassName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改分类："+zjfGoodsClass.getClassName());
				}
				
				zjfGoodsClass.setUpdateTime(DateTime.getDateTime());
				zjfGoodsClass.setUpdateUser(tuser.getLoginName());
				this.zjfGoodsClassDAO.update(zjfGoodsClass);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewGoodsClass(ZjfGoodsClass zjfGoodsClass){
		try{
			this.zjfGoodsClassDAO.save(zjfGoodsClass);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfGoodsClass getGoodsClassById(String classId){
		return this.zjfGoodsClassDAO.findById(classId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}

	public void setZjfGoodsClassDAO(ZjfGoodsClassDAO zjfGoodsClassDAO) {
		this.zjfGoodsClassDAO = zjfGoodsClassDAO;
	}
	public void setZjfGoodsTypeService(ZjfGoodsTypeService zjfGoodsTypeService) {
		this.zjfGoodsTypeService = zjfGoodsTypeService;
	}
}
